package msg;

import java.sql.*;

import database.*;


public class Msg implements DatabaseState {
	
	public static void main(String[] args) {
		java.util.Date date = new java.util.Date();
		System.out.println(date);
		
		Date sqldate = new Date(date.getTime());
		System.out.println(sqldate);
		System.out.println(sqldate.toString());
		
	}
	
	
	private int id;
	private int from_id;
	private boolean isBoardcast;
	private int to_id;
	private String title;
	private String content;
	private java.util.Date dt;
	
	public Msg() {
		// TODO Auto-generated constructor stub
	}
	
	public Msg(int id) {
		this.id = id;
	}
	
	public int sendMsg() {

		try {
			Connection connection = Database.getConnection();
			
			PreparedStatement pstmt = connection.prepareStatement(
							String.format("insert into msg(" +
							"`from_id`, `is_boardcast`, `to_id`, " +
							"`title`, `content`, `dt`) values(" +
							"%d, ?, %d, '%s', '%s', ?)",
							from_id, to_id, title, content));
			
			pstmt.setBoolean(1, isBoardcast);
			dt = new java.util.Date();
			pstmt.setDate(2, new Date(dt.getTime()));
			pstmt.executeUpdate();
			
			pstmt.close();
			connection.close();

			id = Database.getLastId();
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return UNKNOWED_ERROR;
		}
		
		return SUCCESSFUL;
	}
	
	public void deleteMsg() {
		try {
			Connection connection = Database.getConnection();
			
			Statement stmt = connection.createStatement();
			
			stmt.executeUpdate(String.format(
							"delete " +
							"from msg " +
							"where id = '%d'",
							id));
			
			stmt.close();
			connection.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	public void save() {
		try {
			Connection connection = Database.getConnection();
			
			Statement stmt = connection.createStatement();
			
			stmt.executeUpdate(String.format(
							"update msg " +
							"set title = '%s', content = '%s' " +
							"where id = '%d'",
							title, content, id));

			stmt.close();
			connection.close();
					
		} catch (SQLException e) {
			e.printStackTrace();
		}		
	}
	
	public void update() {
		try {
			Connection connection = Database.getConnection();
			
			Statement stmt = connection.createStatement();
			
			ResultSet rs = stmt.executeQuery(String.format(
							"select * " +
							"from msg " +
							"where id = '%d'",
							id));

			if ( rs.next() ) {
				from_id = rs.getInt("from_id");
				to_id = rs.getInt("to_id");
				title = rs.getString("title");
				content = rs.getString("content");
				isBoardcast = rs.getBoolean("is_boardcast");
				
			} else {
				title = null;
			}
			stmt.close();
			connection.close();
					
		} catch (SQLException e) {
			e.printStackTrace();
		}		
	}
	
	public boolean isBoardcast() {
		return isBoardcast;
	}
	
	public String getTo() {
		String to = null;
		
		try {
			Connection connection = Database.getConnection();
			
			Statement stmt = connection.createStatement();
			
			ResultSet rs = stmt.executeQuery(String.format(
							"select usr.login login " +
							"from msg, usr " +
							"where msg.to_id = usr.id and msg.id = %d",
							id));

			if ( rs.next() ) {
				to = rs.getString("login");
			} 
			
			stmt.close();
			connection.close();
					
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return to;
	}
	
	public String getFrom() {
		String from = null;
		
		try {
			Connection connection = Database.getConnection();
			
			Statement stmt = connection.createStatement();
			
			ResultSet rs = stmt.executeQuery(String.format(
							"select usr.login login " +
							"from msg, usr " +
							"where msg.from_id = usr.id and msg.id = %d",
							id));

			if ( rs.next() ) {
				from = rs.getString("login");
			} 
			
			stmt.close();
			connection.close();
					
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return from;	
	}
	
	public int getId() {
		return id;
	}
	
	public int getTo_id() {
		return to_id;
	}
	
	public int getFrom_id() {
		return from_id;
	}
	
	public String getContent() {
		return content;
	}
	
	public String getTitle() {
		return title;
	}
	
	public void setId(int id) {
		this.id = id;
	}
	
	public void setBoardcast(boolean isBoardcast) {
		this.isBoardcast = isBoardcast;
	}
	
	public void setTitle(String title) {
		this.title = title;
	}
	
	public void setContent(String content) {
		this.content = content;
	}
	
	public void setDt(Date dt) {
		this.dt = dt;
	}
	
	public void setTo_id(int to_id) {
		this.to_id = to_id;
	}
	
	public void setFrom_id(int from_id) {
		this.from_id = from_id;
	}
	
	public java.util.Date getDt() {
		return dt;
	}
}
